﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using DBService.Properties;
using YArchitech.LIB.YJKControls;

namespace DBService
{
	public partial class DlgServerManager : System.Windows.Forms.Form
	{
		public static DlgServerManager GetInstance()
		{
			if (DlgServerManager.main == null || DlgServerManager.main.IsDisposed)
			{
				DlgServerManager.main = new DlgServerManager();
			}
			return DlgServerManager.main;
		}

		public void WhenDepartmentChange()
		{
			try
			{
				this.dgvRight.Rows.Clear();
			}
			catch
			{
				this.dgvRight.DataSource = null;
			}
			if (this.currentDepartment != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.btnAddCategory.Enabled = true;
					this.btnDelCategory.Enabled = true;
					this.btnAddUser.Enabled = true;
					this.btnDelUser.Enabled = false;
					this.btnEditUser.Enabled = false;
				}
				this.toolStripStatusLabel1.Text = this.CurrentDepartment.Name;
				this.ViewUser();
				return;
			}
			this.btnAddUser.Enabled = false;
		}

		private void ViewUser()
		{
			try
			{
				List<DbUser> list = new List<DbUser>();
				if (this.currentDepartment.Id == 1)
				{
					list = DbUser.GetAllUsers(BimServerDB.Default);
					DbUser user = DbUser.GetUser("admin", BimServerDB.Default);
					DbUser user2 = DbUser.GetUser("FM", BimServerDB.Default);
					DbUser user3 = DbUser.GetUser("PM", BimServerDB.Default);
					DbUser user4 = DbUser.GetUser("SYSTEM", BimServerDB.Default);
					list.Remove(user);
					list.Remove(user2);
					list.Remove(user3);
					list.Remove(user4);
				}
				else
				{
					list = this.currentDepartment.GetAllUsers();
				}
				this.lvUser.Items.Clear();
				this.lvUser.BeginUpdate();
				foreach (DbUser dbUser in list)
				{
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = dbUser.Name;
					listViewItem.Tag = dbUser;
					ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = dbUser.ChineseName;
					listViewItem.SubItems.Add(listViewSubItem);
					ListViewItem.ListViewSubItem listViewSubItem2 = new ListViewItem.ListViewSubItem();
					listViewSubItem2.Text = this.currentDepartment.Name;
					listViewItem.SubItems.Add(listViewSubItem2);
					ListViewItem.ListViewSubItem listViewSubItem3 = new ListViewItem.ListViewSubItem();
					listViewSubItem3.Text = dbUser.GetCount().ToString();
					listViewItem.SubItems.Add(listViewSubItem3);
					ListViewItem.ListViewSubItem listViewSubItem4 = new ListViewItem.ListViewSubItem();
					listViewSubItem4.Text = dbUser.Email;
					listViewItem.SubItems.Add(listViewSubItem4);
					ListViewItem.ListViewSubItem listViewSubItem5 = new ListViewItem.ListViewSubItem();
					listViewSubItem5.Text = dbUser.Remark;
					listViewItem.SubItems.Add(listViewSubItem5);
					this.lvUser.Items.Add(listViewItem);
				}
				this.lvUser.EndUpdate();
				this.lvUser.Focus();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public void WhenUserChange()
		{
			if (this.CurrentUser != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.btnDelUser.Enabled = true;
					this.btnEditUser.Enabled = true;
				}
				this.ShowRight();
				return;
			}
			this.btnDelUser.Enabled = false;
			this.btnEditUser.Enabled = false;
		}

		public DbDepartment CurrentDepartment
		{
			get
			{
				return this.currentDepartment;
			}
			set
			{
				this.currentDepartment = value;
				this.WhenDepartmentChange();
			}
		}

		public DbUser CurrentUser
		{
			get
			{
				return this.currentUser;
			}
			set
			{
				this.currentUser = value;
				this.WhenUserChange();
			}
		}

		public DlgServerManager()
		{
			this.InitializeComponent();
		}

		private void FrmMain_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.CurrentManager == null)
				{
					DlgAdminLogin dlgAdminLogin = new DlgAdminLogin();
					if (dlgAdminLogin.ShowDialog() == DialogResult.OK)
					{
						this.CurrentManager = dlgAdminLogin.User;
						this.toolStripStatusLabel2.Text = "当前登录帐号：" + this.CurrentManager.ToString();
						if (this.CurrentManager.IsAdmin() || this.CurrentManager.Name == "admin")
						{
							this.mGroup = "SYS";
						}
						else if (this.CurrentManager.IsFamilyManager())
						{
							this.mGroup = "FM";
						}
						else if (this.CurrentManager.IsProjectManager())
						{
							this.mGroup = "PM";
						}
					}
					else
					{
						base.Close();
					}
				}
				this.trDepartment.AfterLabelEdit += this.trCategory_AfterLabelEdit;
				this.trDepartment.ItemDrag += this.trFamilyTree_ItemDrag;
				this.lvUser.ItemDrag += this.listView1_ItemDrag;
				this.lvUser.DragEnter += this.listView1_DragEnter;
				this.lvUser.AfterLabelEdit += this.listView1_AfterLabelEdit;
				this.trDepartment.DragEnter += this.trFamilyTree_DragEnter;
				this.trDepartment.DragOver += this.trFamilyTree_DragOver;
				this.trDepartment.MouseClick += this.trFamilyTree_MouseClick;
				this.trDepartment.DragDrop += this.trFamilyTree_DragDrop;
				this.trDepartment.MouseDown += this.trFamilyTree_MouseDown;
				DbDepartment.FillDepartmentTree(BimServerDB.Default, this.trDepartment);
				string executablePath = Application.ExecutablePath;
				string text = Path.Combine(executablePath.Substring(0, executablePath.LastIndexOf('\\')), "盈建科服务器管理.chm");
				if (!File.Exists(text))
				{
                    //Assist.FileUtil.WriteFile(Resources.盈建科服务器管理, text);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void dgvType_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
		}

		private void listView1_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Move;
		}

		private void trFamilyTree_ItemDrag(object sender, ItemDragEventArgs e)
		{
			if (this.lvUser.SelectedItems.Count > 0)
			{
				base.DoDragDrop(e.Item, DragDropEffects.Move);
			}
		}

		private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
		{
			this.lvUser.DoDragDrop(this.lvUser.SelectedItems[0], DragDropEffects.Copy | DragDropEffects.Move);
		}

		private void trFamilyTree_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(typeof(TreeNode)))
			{
				e.Effect = DragDropEffects.Move;
				return;
			}
			if ((ListViewItem)e.Data.GetData(typeof(ListViewItem)) != null)
			{
				e.Effect = DragDropEffects.Move;
				return;
			}
			this.Cursor = Cursors.No;
		}

		private void trFamilyTree_DragOver(object sender, DragEventArgs e)
		{
			this.Cursor = Cursors.Hand;
		}

		private void trFamilyTree_DragDrop(object sender, DragEventArgs e)
		{
		}

		private void trFamilyTree_MouseClick(object sender, MouseEventArgs e)
		{
			this.trDepartment.ContextMenuStrip = null;
			TreeNode nodeAt = this.trDepartment.GetNodeAt(e.X, e.Y);
			if (nodeAt.Level == 0)
			{
				this.trDepartment.ContextMenuStrip = this.cmsSubject;
				return;
			}
			if (nodeAt.Tag is DbDepartment)
			{
				this.trDepartment.ContextMenuStrip = this.cmsCategory;
			}
		}

		private void btnAddCategory_Click(object sender, EventArgs e)
		{
			this.cmdAddDepartment();
		}

		private void cmdAddDepartment()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.doAddCategory();
					return;
				}
				MessageBox.Show("您的账号没有添加部门的权限。");
			}
		}

		private void doAddCategory()
		{
			if (this.trDepartment.SelectedNode != null)
			{
				DlgAddDepartment dlgAddDepartment = new DlgAddDepartment(this);
				dlgAddDepartment.Activate();
				dlgAddDepartment.ShowDialog(this);
				DbDepartment.FillDepartmentTree(BimServerDB.Default, this.trDepartment);
				return;
			}
			MessageBox.Show("请选择需添加分类的上级节点");
		}

		private void btnDelCategory_Click(object sender, EventArgs e)
		{
			this.cmdDelCategory();
		}

		private void cmdDelCategory()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.doDelCategory();
					return;
				}
				MessageBox.Show("您的账号没有删除部门的权限。");
			}
		}

		private void doDelCategory()
		{
			if (this.CurrentDepartment != null && MessageBox.Show("确实要把选中的分类删除？", "服务器管理", MessageBoxButtons.OKCancel) == DialogResult.OK)
			{
				try
				{
					if (this.CurrentDepartment.Id != 1)
					{
						foreach (DbUser dbUser in this.CurrentDepartment.GetAllUsers())
						{
							dbUser.RemoveFromDb();
						}
						this.CurrentDepartment.RemoveFromDb();
						this.trDepartment.Nodes.Remove(this.trDepartment.SelectedNode);
					}
					else
					{
						MessageBox.Show("禁止删除此节点。");
					}
				}
				catch (Exception)
				{
					MessageBox.Show(this.CurrentDepartment.Name + "删除失败", "服务器管理");
				}
			}
		}

		public TreeNode FindRoot(TreeNode node)
		{
			if (node.Parent == null)
			{
				return node;
			}
			return this.FindRoot(node.Parent);
		}

		private void btnAddFamily_Click(object sender, EventArgs e)
		{
			this.cmdAddFamily();
		}

		private void cmdAddFamily()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.doAddFamily();
					return;
				}
				MessageBox.Show("您的账号没有添加帐号的权限。");
			}
		}

		private void doAddFamily()
		{
			DlgAddUser dlgAddUser = new DlgAddUser(this, this.mGroup);
			if (dlgAddUser.ShowDialog() == DialogResult.OK)
			{
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = dlgAddUser.User.Name;
				listViewItem.Tag = dlgAddUser.User;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = dlgAddUser.User.ChineseName;
				listViewItem.SubItems.Add(listViewSubItem);
				ListViewItem.ListViewSubItem listViewSubItem2 = new ListViewItem.ListViewSubItem();
				listViewSubItem2.Text = this.currentDepartment.Name;
				listViewItem.SubItems.Add(listViewSubItem2);
				ListViewItem.ListViewSubItem listViewSubItem3 = new ListViewItem.ListViewSubItem();
				listViewSubItem3.Text = dlgAddUser.User.GetCount().ToString();
				listViewItem.SubItems.Add(listViewSubItem3);
				ListViewItem.ListViewSubItem listViewSubItem4 = new ListViewItem.ListViewSubItem();
				listViewSubItem4.Text = dlgAddUser.User.Email;
				listViewItem.SubItems.Add(listViewSubItem4);
				ListViewItem.ListViewSubItem listViewSubItem5 = new ListViewItem.ListViewSubItem();
				listViewSubItem5.Text = dlgAddUser.User.Remark;
				listViewItem.SubItems.Add(listViewSubItem5);
				this.lvUser.Items.Add(listViewItem);
				listViewItem.Selected = true;
				this.lvUser.Focus();
			}
		}

		private void Recount()
		{
		}

		private void tsmiAddCategory_Click(object sender, EventArgs e)
		{
			this.doAddCategory();
		}

		private void tsmiDelCategory_Click(object sender, EventArgs e)
		{
			this.doDelCategory();
		}

		private void tsmiAddFamily_Click(object sender, EventArgs e)
		{
			this.cmdAddFamily();
		}

		private void btnEditFamilyParam_Click(object sender, EventArgs e)
		{
			this.cmdEditFamily();
		}

		private void cmdEditFamily()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.doEditFamily();
					return;
				}
				MessageBox.Show("您的账号没有编辑帐号的权限。");
			}
		}

		private void doEditFamily()
		{
			if (this.lvUser.SelectedItems.Count > 0 && new DlgEditUser(this).ShowDialog(this) == DialogResult.OK)
			{
				this.lvUser.SelectedItems[0].Remove();
				DbUser dbUser = this.CurrentUser;
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = dbUser.Name;
				listViewItem.Tag = dbUser;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = dbUser.ChineseName;
				listViewItem.SubItems.Add(listViewSubItem);
				ListViewItem.ListViewSubItem listViewSubItem2 = new ListViewItem.ListViewSubItem();
				listViewSubItem2.Text = this.currentDepartment.Name;
				listViewItem.SubItems.Add(listViewSubItem2);
				ListViewItem.ListViewSubItem listViewSubItem3 = new ListViewItem.ListViewSubItem();
				listViewSubItem3.Text = dbUser.GetCount().ToString();
				listViewItem.SubItems.Add(listViewSubItem3);
				ListViewItem.ListViewSubItem listViewSubItem4 = new ListViewItem.ListViewSubItem();
				listViewSubItem4.Text = dbUser.Email;
				listViewItem.SubItems.Add(listViewSubItem4);
				ListViewItem.ListViewSubItem listViewSubItem5 = new ListViewItem.ListViewSubItem();
				listViewSubItem5.Text = dbUser.Remark;
				listViewItem.SubItems.Add(listViewSubItem5);
				this.lvUser.Items.Add(listViewItem);
				listViewItem.Selected = true;
				this.lvUser.Focus();
			}
		}

		private void trCategory_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
		{
			TreeNode node = e.Node;
			string text = node.Text;
			Regex regex = new Regex("^[0-9a-zA-Z一-龥]+$");
			if (!text.Equals(e.Label) && e.Label != null && e.Label.Trim() != "" && regex.IsMatch(e.Label) && node.Tag is DbDepartment)
			{
				DbDepartment dbDepartment = (DbDepartment)node.Tag;
				try
				{
					dbDepartment.Name = e.Label;
					dbDepartment.UpdateToDb();
					node.Text = e.Label;
				}
				catch (Exception)
				{
					node.Text = text;
					MessageBox.Show("重命名发生异常，请检查是否有重名。");
				}
			}
			e.CancelEdit = true;
			this.trDepartment.LabelEdit = false;
		}

		private void trFamilyTree_AfterSelect(object sender, TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			if (node.Tag != null && node.Tag is DbDepartment)
			{
				this.CurrentDepartment = (DbDepartment)this.FindRoot(node).Tag;
				this.CurrentDepartment = (DbDepartment)node.Tag;
				this.CurrentUser = null;
			}
		}

		private void doAddType()
		{
		}

		private void btnAddType_Click(object sender, EventArgs e)
		{
			this.cmdAddType();
		}

		private void cmdAddType()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.CanAddFamily())
				{
					this.doAddType();
					return;
				}
				MessageBox.Show("您的账号没有添加规格的权限。");
			}
		}

		private void btnDelFamily_Click(object sender, EventArgs e)
		{
			this.cmdDelFamily();
			this.lvUser.Focus();
		}

		private void cmdLogin()
		{
			DlgLogin dlgLogin = new DlgLogin();
			if (dlgLogin.ShowDialog(this) == DialogResult.OK && dlgLogin.User != null)
			{
				dlgLogin.User.IncreaseCount();
				this.CurrentManager = dlgLogin.User;
				this.tsslCurrentUser.Text = "";
				this.toolStripStatusLabel2.Text = string.Format("当前登录用户：{0}", this.CurrentManager.Name);
				this.initPermission();
			}
		}

		private void cmdDelFamily()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.IsAdmin())
				{
					this.doRemoveUser();
					this.dgvRight.DataSource = null;
					return;
				}
				MessageBox.Show("您的账号没有删除帐号的权限。");
			}
		}

		private void doRemoveUser()
		{
			if (this.lvUser.SelectedItems.Count > 0 && this.lvUser.SelectedItems.Count > 0 && MessageBox.Show("确定要删除选中的用户？", "服务器管理", MessageBoxButtons.OKCancel) == DialogResult.OK)
			{
				this.lvUser.BeginUpdate();
				foreach (object obj in this.lvUser.SelectedItems)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					try
					{
						((DbUser)listViewItem.Tag).RemoveFromDb();
						listViewItem.Remove();
					}
					catch (Exception ex)
					{
						MessageBox.Show(listViewItem.Name + "删除失败!", "族库管理");
						MessageBox.Show(ex.Message);
					}
				}
				this.lvUser.EndUpdate();
				this.dgvRight.Rows.Clear();
				this.Recount();
			}
		}

		private void tsmiDelFamily_Click(object sender, EventArgs e)
		{
			this.cmdDelFamily();
		}

		private void doEditType()
		{
		}

		private void btnEditType_Click(object sender, EventArgs e)
		{
			this.cmdEditType();
		}

		private void cmdEditType()
		{
		}

		private void tsmiAddType_Click(object sender, EventArgs e)
		{
			this.doAddType();
		}

		private void tsmiEditType_Click(object sender, EventArgs e)
		{
			this.doEditType();
		}

		private void tsmiDelType_Click(object sender, EventArgs e)
		{
		}

		private void btnDelType_Click(object sender, EventArgs e)
		{
			this.cmdDelType();
		}

		private void cmdDelType()
		{
		}

		private void tsmiEditFamilyParams_Click(object sender, EventArgs e)
		{
		}

		private void btnSelectType_Click(object sender, EventArgs e)
		{
		}

		private void AddCategoryToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void trFamilyTree_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				TreeNode nodeAt = this.trDepartment.GetNodeAt(e.X, e.Y);
				if (nodeAt != null)
				{
					this.trDepartment.SelectedNode = nodeAt;
				}
			}
		}

		private void ReNameToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.trDepartment.LabelEdit = true;
			this.trDepartment.SelectedNode.BeginEdit();
		}

		private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
		{
			ListView listView = sender as ListView;
			if (e.Column != (listView.ListViewItemSorter as DlgServerManager.ListViewColumnSorter).ColumnToSort)
			{
				(listView.ListViewItemSorter as DlgServerManager.ListViewColumnSorter).ColumnToSort = e.Column;
				(listView.ListViewItemSorter as DlgServerManager.ListViewColumnSorter).OrderOfSort = SortOrder.Ascending;
				return;
			}
			if ((listView.ListViewItemSorter as DlgServerManager.ListViewColumnSorter).OrderOfSort == SortOrder.Ascending)
			{
				(listView.ListViewItemSorter as DlgServerManager.ListViewColumnSorter).OrderOfSort = SortOrder.Descending;
				return;
			}
			(listView.ListViewItemSorter as DlgServerManager.ListViewColumnSorter).OrderOfSort = SortOrder.Ascending;
		}

		private void listView1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lvUser.SelectedItems.Count >= 1)
			{
				this.CurrentUser = (DbUser)this.lvUser.SelectedItems[0].Tag;
				this.lvUser.ContextMenuStrip = this.cmsFamily;
				return;
			}
			this.lvUser.ContextMenuStrip = null;
		}

		public void ShowRight()
		{
			try
			{
				this.dgvRight.Rows.Clear();
			}
			catch
			{
				this.dgvRight.DataSource = "";
			}
			List<DbRight> rightList = this.currentUser.GetRightList(this.mGroup);
			foreach (DbRight dbRight in DbRight.GetAllRights(BimServerDB.Default, this.mGroup))
			{
				if (rightList.Contains(dbRight))
				{
					this.dgvRight.Rows.Add(new object[]
					{
						true,
						dbRight.Name,
						dbRight.Description,
						dbRight.Id
					});
				}
				else
				{
					this.dgvRight.Rows.Add(new object[]
					{
						false,
						dbRight.Name,
						dbRight.Description,
						dbRight.Id
					});
				}
			}
		}

		private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
		{
			string text = this.lvUser.Items[e.Item].Text;
			if (this.lvUser.Items[e.Item].Tag is DbUser && e.Label != null && e.Label != "" && !text.Equals(e.Label))
			{
				if (((DbUser)this.lvUser.Items[e.Item].Tag).Rename(e.Label))
				{
					this.lvUser.Items[e.Item].Text = e.Label;
				}
				else
				{
					this.lvUser.Items[e.Item].Text = text;
				}
			}
			e.CancelEdit = true;
			this.lvUser.LabelEdit = false;
		}

		private void tsmAbout(object sender, EventArgs e)
		{
		}

		private void inputToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void outputToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdExport();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void helpToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void groupBox3_Enter(object sender, EventArgs e)
		{
		}

		private void btnAddOk_Click(object sender, EventArgs e)
		{
		}

		private void btnAddCancel_Click(object sender, EventArgs e)
		{
		}

		private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void AddTypeToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdAddType();
		}

		private void RemoveTypeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdDelType();
		}

		private void EditTypeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdEditType();
		}

		private void addCategoryToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdAddDepartment();
		}

		private void removeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdDelCategory();
		}

		private void AddFamilyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdAddFamily();
		}

		private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdDelFamily();
		}

		private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdEditFamily();
		}

		private void addFamilyToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdAddFamily();
		}

		private void 添加分类ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.doAddCategory();
		}

		private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.lvUser.SelectedItems.Count > 0)
			{
				this.lvUser.LabelEdit = true;
				this.lvUser.SelectedItems[0].BeginEdit();
			}
		}

		private void btnEditCategoryParam_Click(object sender, EventArgs e)
		{
			this.cmdEditCategory();
		}

		private void cmdEditCategory()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.CanAddFamily())
				{
					this.doEditCategory();
					return;
				}
				MessageBox.Show("您的账号没有编辑分类的权限。");
			}
		}

		private void doEditCategory()
		{
		}

		private void editCategoryToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdEditCategory();
		}

		private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdExport();
		}

		private void cmdExport()
		{
			if (this.CurrentManager == null)
			{
				this.cmdLogin();
			}
			if (this.CurrentManager != null)
			{
				if (this.CurrentManager.CanExport())
				{
					this.doExport();
					return;
				}
				MessageBox.Show("您的账号没有导出族库的权限。");
			}
		}

		private void doExport()
		{
		}

		private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void dgvType_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void 登陆服务器ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void 服务器ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void 本地族库ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void dgvType_SortCompare_1(object sender, DataGridViewSortCompareEventArgs e)
		{
			double num;
			double.TryParse(e.CellValue1.ToString().Trim(), out num);
			double num2;
			double.TryParse(e.CellValue2.ToString().Trim(), out num2);
			if (num < num2)
			{
				e.SortResult = -1;
			}
			else if (num == num2)
			{
				e.SortResult = 0;
			}
			else
			{
				e.SortResult = 1;
			}
			e.Handled = true;
		}

		private void dgvType_Sorted(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void button3_Click(object sender, EventArgs e)
		{
		}

		private void button4_Click(object sender, EventArgs e)
		{
		}

		private void button2_Click(object sender, EventArgs e)
		{
		}

		private void button5_Click(object sender, EventArgs e)
		{
		}

		private void dgvType_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			MessageBox.Show("输入的格式不正确，请检查后重新输入。");
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
		}

		private void tstbSearchName_Enter(object sender, EventArgs e)
		{
		}

		private void tstbSearchName_Click(object sender, EventArgs e)
		{
		}

		private void tstbSearchName_KeyDown(object sender, KeyEventArgs e)
		{
		}

		private void tsmiServer_Click(object sender, EventArgs e)
		{
		}

		private void autoLogin()
		{
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
			DbUser user = DbUser.GetUser(XmlUtils.GetDataFromConfigFile(fileName, "AutoUser", "value"), BimServerDB.Default);
			if (user == null)
			{
				return;
			}
			string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "AutoPwd", "value");
			if (!user.Password.Equals(DbUser.ToMd5(dataFromConfigFile)))
			{
				return;
			}
			this.CurrentManager = user;
			user.IncreaseCount();
			this.tsslCurrentUser.Text = "";
			this.toolStripStatusLabel2.Text = string.Format("当前登录用户：{0}", this.CurrentManager.Name);
			this.initPermission();
		}

		private void toolStripDropDownButton1_Click(object sender, EventArgs e)
		{
		}

		private void tsmiLogin_Click(object sender, EventArgs e)
		{
		}

		private void cmdLogout()
		{
			this.CurrentManager = null;
			this.toolStripStatusLabel2.Text = "服务器管理账户当前未登录";
			this.tsddbCommunication.Enabled = false;
			this.tsddbAdmin.Visible = false;
			this.tsmChangePass.Visible = false;
			MessageBox.Show("成功注销。");
		}

		private void tsmiLogout_Click(object sender, EventArgs e)
		{
		}

		private void tsmiRegiste_Click(object sender, EventArgs e)
		{
		}

		private void tsmiChangePW_Click(object sender, EventArgs e)
		{
		}

		private void 登陆ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void 注册ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgRegistrate(this.mGroup).ShowDialog(this);
		}

		private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdLogout();
		}

		private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgChangePassword(this.CurrentManager.Name).ShowDialog(this);
		}

		private void tsddbLogin_Click(object sender, EventArgs e)
		{
		}

		private void toolStripDropDownButton2_Click(object sender, EventArgs e)
		{
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			try
			{
				if (this.CurrentManager != null)
				{
					if (this.CurrentManager.NewMessageCount > 0)
					{
						this.tsddbCommunication.Text = "通讯(" + this.CurrentManager.NewMessageCount + ")";
						this.tsmiInbox.Text = "收件箱(" + this.CurrentManager.NewMessageCount + ")";
					}
					else
					{
						this.tsddbCommunication.Text = "通讯";
						this.tsmiInbox.Text = "收件箱";
					}
					this.initPermission();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				this.timer1.Enabled = false;
			}
		}

		private void tsmiAddRole_Click(object sender, EventArgs e)
		{
		}

		private void tsmiRemove_Click(object sender, EventArgs e)
		{
			new DlgRemoveRole(this.mGroup).ShowDialog(this);
		}

		private void tsmiSetRight_Click(object sender, EventArgs e)
		{
			new DlgSetRight(this.mGroup).ShowDialog(this);
		}

		private void tsmiSetRole_Click(object sender, EventArgs e)
		{
			new DlgSetRole(this.mGroup).ShowDialog(this);
		}

		private void initPermission()
		{
			this.tsddbAdmin.Visible = false;
			this.tsddbCommunication.Visible = false;
			this.tsmChangePass.Visible = true;
			this.tsddbCommunication.Visible = true;
			this.tsddbCommunication.Enabled = true;
			if (this.CurrentManager != null && (this.CurrentManager.IsFamilyManager() || this.CurrentManager.IsProjectManager() || this.CurrentManager.IsAdmin()))
			{
				this.tsddbAdmin.Visible = true;
			}
		}

		private void tsmiSent_Click(object sender, EventArgs e)
		{
			new DlgSendMessage(this.CurrentManager).ShowDialog(this);
		}

		private void tsmiInbox_Click(object sender, EventArgs e)
		{
			new DlgMessageBox(this.CurrentManager).ShowDialog(this);
		}

		private void tsmiDelUser_Click(object sender, EventArgs e)
		{
			new DlgRemoveUser().ShowDialog(this);
		}

		private void 添加职位ToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			new DlgAddRole(this.mGroup).ShowDialog(this);
		}

		private void 删除职位ToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			new DlgRemoveRole(this.mGroup).ShowDialog(this);
		}

		private void 权限设置ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgSetRight(this.mGroup).ShowDialog(this);
		}

		private void tsmiVerifyUser_Click(object sender, EventArgs e)
		{
		}

		private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgRemoveUser().ShowDialog(this);
		}

		private void 职位设置ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgSetRole(this.mGroup).ShowDialog(this);
		}

		private void toolStripMenuItem4_Click(object sender, EventArgs e)
		{
			new DlgSendMessage(this.CurrentManager).ShowDialog(this);
		}

		private void 收件箱ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgMessageBox(this.CurrentManager).ShowDialog(this);
		}

		private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
		{
		}

		private void 导入设置ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void 注销登陆ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
			XmlUtils.WriteConfigFile(fileName, "AutoUser", "value", "");
			XmlUtils.WriteConfigFile(fileName, "AutoPwd", "value", "");
			base.Close();
			base.Dispose();
		}

		private void button1_Click_1(object sender, EventArgs e)
		{
			try
			{
				if (this.currentUser != null)
				{
					List<DbRight> list = new List<DbRight>();
					foreach (object obj in ((IEnumerable)this.dgvRight.Rows))
					{
						DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
						if ((bool)dataGridViewRow.Cells["Add"].Value)
						{
							list.Add(new DbRight(Convert.ToInt32(dataGridViewRow.Cells["GUID"].Value), dataGridViewRow.Cells["Right"].Value.ToString(), dataGridViewRow.Cells["Desc"].Value.ToString(), this.currentUser.Database));
						}
					}
					List<DbRight> list2 = new List<DbRight>();
					foreach (object obj2 in ((IEnumerable)this.dgvRight.Rows))
					{
						DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
						list2.Add(new DbRight(Convert.ToInt32(dataGridViewRow2.Cells["GUID"].Value), dataGridViewRow2.Cells["Right"].Value.ToString(), dataGridViewRow2.Cells["Desc"].Value.ToString(), this.currentUser.Database));
					}
					this.currentUser.UpdateRight(this.currentUser, list2, list);
					MessageBox.Show("更新成功。");
				}
				else
				{
					MessageBox.Show("请选择用户。");
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.trDepartment.LabelEdit = true;
			this.trDepartment.SelectedNode.BeginEdit();
		}

		private void button2_Click_1(object sender, EventArgs e)
		{
			DlgImportUser dlgImportUser = new DlgImportUser();
			if (dlgImportUser.ShowDialog() == DialogResult.OK)
			{
				try
				{
					List<AdModel> list = DomainUtils.ReadDomain(dlgImportUser.Ip, dlgImportUser.User, dlgImportUser.Pwd);
					foreach (AdModel adModel in list)
					{
						try
						{
							if (adModel.TypeId == 1)
							{
								DlgServerManager.AddDepartment(list, adModel);
							}
						}
						catch
						{
						}
					}
					foreach (AdModel adModel2 in list)
					{
						if (adModel2.TypeId == 2)
						{
							try
							{
								AdModel adModel3 = null;
								foreach (AdModel adModel4 in list)
								{
									if (adModel2.ParentId == adModel4.Id)
									{
										adModel3 = adModel4;
										break;
									}
								}
								if (adModel3 != null)
								{
									DbDepartment department = DbDepartment.GetDepartment(BimServerDB.Default, adModel3.Name);
									DbUser dbUser = new DbUser(adModel2.Name, "", "123", "");
									dbUser.AddToDb(BimServerDB.Default);
									dbUser.ToDepartment(department.Id);
								}
							}
							catch (Exception)
							{
							}
						}
					}
					MessageBox.Show("导入完成。");
					DbDepartment.FillDepartmentTree(BimServerDB.Default, this.trDepartment);
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "族库管理");
				}
			}
		}

		private static void AddDepartment(List<AdModel> list, AdModel am)
		{
			AdModel adModel = null;
			foreach (AdModel adModel2 in list)
			{
				if (am.ParentId == adModel2.Id)
				{
					adModel = adModel2;
					break;
				}
			}
			if (adModel == null)
			{
				DbDepartment department = DbDepartment.GetDepartment(BimServerDB.Default, 1);
				DbDepartment department2 = new DbDepartment(am.Name, am.Name, 0);
				department.AddSubDepartment(department2);
				return;
			}
			DbDepartment department3 = DbDepartment.GetDepartment(BimServerDB.Default, adModel.Name);
			if (department3 != null)
			{
				DbDepartment department4 = new DbDepartment(am.Name, am.Name, 0);
				department3.AddSubDepartment(department4);
				return;
			}
			DlgServerManager.AddDepartment(list, adModel);
			DlgServerManager.AddDepartment(list, am);
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
		}

		private void 修改密码ToolStripMenuItem_Click_1(object sender, EventArgs e)
		{
			new DlgChangePassword(this.CurrentManager.Name).ShowDialog(this);
		}

		private void lvUser_MouseUp(object sender, MouseEventArgs e)
		{
			if (this.lvUser.SelectedItems.Count == 0 && this.lvUser.FocusedItem != null && this.lvUser.GetItemAt(e.X, e.Y) == null)
			{
				this.lvUser.FocusedItem.Selected = true;
			}
		}

		private void tsddbHelp_Click(object sender, EventArgs e)
		{
			string executablePath = Application.ExecutablePath;
			Process.Start(Path.Combine(executablePath.Substring(0, executablePath.LastIndexOf('\\')), "盈建科服务器管理.chm"));
		}

		private static DlgServerManager main;

		private string mGroup = "";

		public DbDepartment currentDepartment;

		public DbUser currentUser;

		public string initialDirectory = "C:\\";

		private int addRowsNo;

		private DbUser CurrentManager;

		public class ListViewColumnSorter : IComparer
		{
			public ListViewColumnSorter()
			{
				this.ColumnToSort = 0;
				this.OrderOfSort = SortOrder.None;
				this.ObjectCompare = new CaseInsensitiveComparer();
			}

			public int Compare(object x, object y)
			{
				return 0;
			}

			public int ColumnToSort;

			public SortOrder OrderOfSort;

			private CaseInsensitiveComparer ObjectCompare;
		}
	}
}
